﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SC2parser.Model
{
    public class Match : IEquatable<Match>
    {
        private string tournamentName;
        private int matchId;
        private Player playerOne;
        private Player playerTwo;

        public Player PlayerOne
        {
            get { return playerOne; }
        }

        public Player PlayerTwo
        {
            get { return playerTwo; }
        }

        /// <summary>
        /// Instantiates a new match with two players and a matchID
        /// </summary>
        /// <param name="playerOne">The first player</param>
        /// <param name="playerTwo">The second player</param>
        /// <param name="matchId">The ID of the match</param>
        public Match(Player playerOne, Player playerTwo, int matchId)
        {
            this.playerOne = playerOne;
            this.playerTwo = playerTwo;
            this.matchId = matchId;
        }

        public int MatchId
        {
            get { return matchId; }
            set { matchId = value; }
        }

        public string TournamentName
        {
            get { return tournamentName; }
            set { tournamentName = value; }
        }

        public Player winner()
        {
            if (playerOne.Wins > playerTwo.Wins)
                return playerOne;
            else return playerTwo;
        }

        /// <summary>
        /// A string describing the match
        /// </summary>
        /// <returns></returns>
        public string info()
        {
            return playerOne.Race + " " + playerOne.Name + " from: " + PlayerOne.Country + " " + playerOne.Wins + " VS " + playerTwo.Wins + " " + playerTwo.Name + " from: " + PlayerTwo.Country + " " + playerTwo.Race;
        }

        /// <summary>
        /// Gets a hashcode of the match
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return playerOne.GetHashCode() ^
                playerTwo.GetHashCode() ^
                matchId.GetHashCode() ^
                tournamentName.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;
            if (this.GetType() != obj.GetType())
                return false;
            return Equals((Match)obj);
        }

        /// <summary>
        /// Determines if the object is equal to another Match object
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public bool Equals(Match other)
        {
            if (other == null)
                return false;
            if (ReferenceEquals(this, other))
                return true;
            if (GetHashCode() != other.GetHashCode())
                return false;
            return (playerOne.Equals(other.playerOne) &&
                playerTwo.Equals(other.playerTwo) &&
                matchId.Equals(other.matchId) &&
                tournamentName.Equals(other.tournamentName));
        }
    }
}
